package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Trent;
import com.xy.manage.service.TrentService;



@Controller
public class TrentAction{
	private static final Logger log = LoggerFactory.getLogger(TrentAction.class);
	public TrentAction() {
	}
 
 
 	/**
	 * 租房信息新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_trentAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("trent", new Trent());
        setDict(model);
		return "trentEdit";
	}

	/**
	 * 租房信息新增保存
	 * 
	 * @param trent
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_trentSave.do")
	public String save(Trent trent, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(trent, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		trent = trentService.save(trent);
		return "redirect:v_trentList.do";
	}

	/**
	 * 租房信息删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_trentDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				trentService.remove(id);
			}
		}
		return "redirect:v_trentList.do";
	}

	/**
	 * 租房信息编辑页面
	 * 
	 * @param rentid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_trentEdit.do")
	public String edit(long rentid, HttpServletRequest request, ModelMap model) {
		Trent trent = trentService.get(rentid);
		model.addAttribute("trent", trent);
      model.addAttribute("actionEdit", request.getParameter("request"));
        setDict(model);
		return "trentEdit";
	}

	/**
	 * 租房信息详情
	 * 
	 * @param rentid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_trentDetail.do")
	public String detail(long rentid, HttpServletRequest request, ModelMap model) {
		Trent trent = trentService.get(rentid);
		model.addAttribute("trent", trent);
        setDict(model);
		return "trentView";
	}

	/**
	 * 租房信息查询（分页）
	 * 
	 * @param trent
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_trentList.do")
	public String listPage(Trent trent, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = trentService.getTrentByPage(toMap(trent),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "trentList";
	}

	/**
	 * 租房信息查询
	 * 
	 * @param trent
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_trentList1.do")
	public String list(Trent trent, HttpServletRequest request, ModelMap model) {
		List<Trent> list = trentService.getTrent(toMap(trent));
		model.addAttribute("list", list);
        setDict(model);
		return "trentList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param trent
	 * @return
	 */	private Map<String,Object> toMap(Trent trent){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(trent.getRentid()!=null&&!trent.getRentid().equals("")){
			map.put("Rentid", trent.getRentid());
		}
		if(trent.getUsername()!=null&&!trent.getUsername().equals("")){
			map.put("Username", trent.getUsername());
		}
		if(trent.getRoomNo()!=null&&!trent.getRoomNo().equals("")){
			map.put("RoomNo", trent.getRoomNo());
		}
		if(trent.getSSQ()!=null&&!trent.getSSQ().equals("")){
			map.put("SSQ", trent.getSSQ());
		}
		if(trent.getXZJD()!=null&&!trent.getXZJD().equals("")){
			map.put("XZJD", trent.getXZJD());
		}
		if(trent.getJWH()!=null&&!trent.getJWH().equals("")){
			map.put("JWH", trent.getJWH());
		}
		if(trent.getJLX()!=null&&!trent.getJLX().equals("")){
			map.put("JLX", trent.getJLX());
		}
		if(trent.getJLXName()!=null&&!trent.getJLXName().equals("")){
			map.put("JLXName", trent.getJLXName());
		}
		if(trent.getMPH()!=null&&!trent.getMPH().equals("")){
			map.put("MPH", trent.getMPH());
		}
		if(trent.getMPHMC()!=null&&!trent.getMPHMC().equals("")){
			map.put("MPHMC", trent.getMPHMC());
		}
		if(trent.getXZ()!=null&&!trent.getXZ().equals("")){
			map.put("XZ", trent.getXZ());
		}
		if(trent.getQZ()!=null&&!trent.getQZ().equals("")){
			map.put("QZ", trent.getQZ());
		}
		if(trent.getPCS()!=null&&!trent.getPCS().equals("")){
			map.put("PCS", trent.getPCS());
		}
		if(trent.getSQ()!=null&&!trent.getSQ().equals("")){
			map.put("SQ", trent.getSQ());
		}
		if(trent.getWZ()!=null&&!trent.getWZ().equals("")){
			map.put("WZ", trent.getWZ());
		}
		if(trent.getLXDH()!=null&&!trent.getLXDH().equals("")){
			map.put("LXDH", trent.getLXDH());
		}
		if(trent.getFWLB()!=null&&!trent.getFWLB().equals("")){
			map.put("FWLB", trent.getFWLB());
		}
		if(trent.getFWJS()!=null&&!trent.getFWJS().equals("")){
			map.put("FWJS", trent.getFWJS());
		}
		if(trent.getFWYT()!=null&&!trent.getFWYT().equals("")){
			map.put("FWYT", trent.getFWYT());
		}
		if(trent.getFWMJ()!=null&&!trent.getFWMJ().equals("")){
			map.put("FWMJ", trent.getFWMJ());
		}
		if(trent.getYHLB()!=null&&!trent.getYHLB().equals("")){
			map.put("YHLB", trent.getYHLB());
		}
		if(trent.getFWZLH()!=null&&!trent.getFWZLH().equals("")){
			map.put("FWZLH", trent.getFWZLH());
		}
		if(trent.getFWCQXZ()!=null&&!trent.getFWCQXZ().equals("")){
			map.put("FWCQXZ", trent.getFWCQXZ());
		}
		if(trent.getFWCQZH()!=null&&!trent.getFWCQZH().equals("")){
			map.put("FWCQZH", trent.getFWCQZH());
		}
		if(trent.getZHZRS()!=null&&!trent.getZHZRS().equals("")){
			map.put("ZHZRS", trent.getZHZRS());
		}
		if(trent.getFWGLLB()!=null&&!trent.getFWGLLB().equals("")){
			map.put("FWGLLB", trent.getFWGLLB());
		}
		if(trent.getHGYXM()!=null&&!trent.getHGYXM().equals("")){
			map.put("HGYXM", trent.getHGYXM());
		}
		if(trent.getIsCZFW()!=null&&!trent.getIsCZFW().equals("")){
			map.put("IsCZFW", trent.getIsCZFW());
		}
		if(trent.getCZZT()!=null&&!trent.getCZZT().equals("")){
			map.put("CZZT", trent.getCZZT());
		}
		if(trent.getQZRQ()!=null&&!trent.getQZRQ().equals("")){
			map.put("QZRQ", trent.getQZRQ());
		}
		if(trent.getTZRQ()!=null&&!trent.getTZRQ().equals("")){
			map.put("TZRQ", trent.getTZRQ());
		}
		if(trent.getCreatetime()!=null&&!trent.getCreatetime().equals("")){
			map.put("Createtime", trent.getCreatetime());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("SSQMap", DictUtils.getDictByType(DictUtils.TRENT_SSQ));
      model.addAttribute("PCSMap", DictUtils.getDictByType(DictUtils.TRENT_PCS));
      model.addAttribute("FWLBMap", DictUtils.getDictByType(DictUtils.TRENT_FWLB));
      model.addAttribute("YHLBMap", DictUtils.getDictByType(DictUtils.TRENT_YHLB));
      model.addAttribute("FWGLLBMap", DictUtils.getDictByType(DictUtils.TRENT_FWGLLB));
      model.addAttribute("isCZFWMap", DictUtils.getDictByType(DictUtils.TRENT_ISCZFW));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Trent trent, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TrentService trentService;   
}